package com.cby.android.sample.activities

import android.graphics.Color
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.cby.android.library.base.BaseStateActivity
import com.cby.android.library.ext.observe
import com.cby.android.sample.R
import com.cby.android.sample.adapter.RollHistoryTodayAdapter
import com.cby.android.sample.bean.roll.RollHistoryBean
import com.cby.android.sample.intent.RollMoreIntent
import com.cby.android.sample.model.RollMoreViewModel
import com.cby.android.sample.state.RollMoreState
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration
import kotlinx.coroutines.launch

/**
 * @description: RollHistoryTodayActivity
 *
 * @author ChenBaoyang
 * @date 4/24/21 10:54
 */
class RollHistoryTodayActivity : BaseStateActivity<RollMoreViewModel, RollMoreState, RollMoreIntent>() {

    private val recyclerView: RecyclerView by lazy { findViewById(R.id.recycler_view) }
    private val refreshLayout: RefreshLayout by lazy { findViewById(R.id.refreshLayout) }

    private lateinit var adapter: RollHistoryTodayAdapter

    override fun bindLiveData() {
        observe(viewModel!!.state) {
            adapter.setNewInstance(it.dataHistoryToday as MutableList<RollHistoryBean>)
        }
    }

    override fun onViewInit() {
        title = "历史上的今天"
        refreshLayout.setEnableLoadMore(false)
        refreshLayout.setEnableRefresh(false)


        val linearLayoutManager =
            LinearLayoutManager(
                this,
                LinearLayoutManager.VERTICAL,
                false
            )
        recyclerView.layoutManager = linearLayoutManager
        recyclerView.itemAnimator = DefaultItemAnimator()
        recyclerView.addItemDecoration(
            HorizontalDividerItemDecoration.Builder(this).color(Color.LTGRAY)
                .build()
        )

        adapter = RollHistoryTodayAdapter()
        recyclerView.adapter = adapter
    }

    override fun reload() {
        lifecycleScope.launch {
            viewModel!!.intents.send(RollMoreIntent.LoadRollHistoryIntent)
        }
    }

    override fun getLayoutId() = R.layout.common_activity_recycler_view

    override fun onDataInit() {
        super.onDataInit()
        reload()
    }

    override fun getTargetView() = refreshLayout

}